package com.tsingtree.sys.core.util;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.tsingtree.base.consts.ConstantsContext;
import com.tsingtree.roses.core.util.ToolUtil;
import com.tsingtree.roses.kernel.model.exception.ServiceException;
import com.tsingtree.sys.core.exception.enums.BizExceptionEnum;
import com.tsingtree.sys.core.listener.ConfigListener;
import com.tsingtree.sys.modular.system.model.UeditorFileResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 百度富文本的工具类
 *
 * @author jagger
 */
@Slf4j
public class UeditorUtil {

    /**
     * ue上传文件逻辑
     *
     * @author jagger
     */
    public static UeditorFileResult uploadFile(MultipartFile upfile, FileType fileType, long userId) {
        if (upfile.isEmpty()) {
            throw new ServiceException(BizExceptionEnum.UE_FILE_NULL_ERROR);
        }

        // 获取文件名,后缀名
        String oldFileName = upfile.getOriginalFilename();
        String suffixName = ToolUtil.getFileSuffix(oldFileName);

        // 重新命名图片
        String newFileName = "/upload/admin/ueditor/" + userId + "/" + IdWorker.getIdStr() + "." + suffixName;

        UeditorFileResult ueditorFileResult = new UeditorFileResult();
        String path;
        // 如果是上传图片
        if (fileType.equals(FileType.IMG)) {

            ueditorFileResult.setUrl(UeditorUtil.getImageRelativeUrl(newFileName));
            ueditorFileResult.setTitle(newFileName);
            ueditorFileResult.setOriginal(newFileName);

            //文件用原始文件
            path = ConstantsContext.getFileUploadPath() + newFileName;


        } else if (fileType.equals(FileType.FILE)) {

            // 如果是上传文件
            ueditorFileResult.setUrl(UeditorUtil.getFileRelativeUrl(newFileName) + "/" + oldFileName);
            ueditorFileResult.setTitle(oldFileName);
            ueditorFileResult.setOriginal(oldFileName);

            //文件用原始文件
            path = ConstantsContext.getFileUploadPath()  + newFileName;

        } else {

            // 如果是上传视频
            ueditorFileResult.setUrl(UeditorUtil.getVideoRelativeUrl(newFileName));
            ueditorFileResult.setTitle(newFileName);
            ueditorFileResult.setOriginal(newFileName);

            //文件用原始文件
            path = ConstantsContext.getFileUploadPath() + newFileName;

        }

        try {

            File dest = new File(path);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }

            upfile.transferTo(dest);
            return ueditorFileResult;
        } catch (IOException e) {
            log.error("保存ue的上传文件出错！", e);
            throw new ServiceException(BizExceptionEnum.UE_FILE_SAVE_ERROR);
        }
    }


    /**
     * 获取图片相对路径
     *
     * @author jagger
     */
    private static String getImageRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + imageName;
    }

    /**
     * 获取文件相对路径
     *
     * @author jagger
     */
    private static String getFileRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + imageName;
    }

    /**
     * 获取视频相对路径
     *
     * @author jagger
     */
    private static String getVideoRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + imageName;
    }

    /**
     * 文件类型
     *
     * @author jagger
     */
    public enum FileType {

        /**
         * 图片类型
         */
        IMG,

        /**
         * 文件类型
         */
        FILE,

        /**
         * 视频类型
         */
        VIDEO
    }

}
